'use strict'

const Ws = require('./lib/ws');
const PlcSocket = require('./lib/socket');
const schedule = require('node-schedule');
const {fork} = require('child_process');
const path = require('path');
class MainThread {

    panelId = null
    constructor () {
        // 初始化与plc连接
        PlcSocket.getAllInstance();
        // 初始化ws
        this.ws = Ws.getInstance();
        // 创建子进程
        this.child_process = fork(path.join(__dirname, 'job/cleanning.js'));
        // 接受子进程的缓存数据
        this.child_process.on('message', (msg) => {
            console.log(msg);
            this.ws.sendPanelInfo(msg.port, msg.info);
        })
        // this.go();
    }
    /**
     * 主线程入口
     */
    go () {
        // 模拟plc信号
        const job = schedule.scheduleJob( '*/5 * * * * *', () => {
            this.child_process.send({port: 1026, panelId: 'PANELID12312312'});
        });
    }
    /**
     * 获取Plc的检测信息
     * @param {Object} data plc返回的端口号 和 panelId
     * @detail {port: this.port, panelId: bufstr}
     */
    getPlcData (data) {
        this.child_process.send(data);
    }
}

let mainThrad = new MainThread();
module.exports = mainThrad;
